package com.kepler.social.web.controller;

import com.kepler.social.app.port.UserMoodPort;
import com.kepler.social.common.constants.BusinessState;
import com.kepler.social.common.constants.MediaType;
import com.kepler.social.common.constants.PostType;
import com.kepler.social.common.constants.TipsConst;
import com.kepler.social.common.utils.Jackson2Helper;
import com.kepler.social.common.utils.ValidateAssert;
import com.kepler.social.domain.port.vo.ResultVo;
import com.kepler.social.generic.SecurityHelper;
import com.kepler.social.service.vo.SysUserVo;
import com.kepler.social.web.common.HttpResult;
import com.kepler.social.web.controller.converter.MoodConverter;
import com.kepler.social.web.controller.param.CreateMoodReq;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Objects;
import java.util.UUID;

@RestController
@RequestMapping("/mood")
@Slf4j
public class MoodController {

    @Resource
    private UserMoodPort userMoodPort;

    @RequestMapping("/create")
    public HttpResult createMediaMood(@RequestBody CreateMoodReq createMoodReq) {
        SysUserVo sysUserVo = SecurityHelper.getSysUserVo();
        ValidateAssert.notNull(createMoodReq, "无效!");
        ValidateAssert.isTrue(PostType.exists(createMoodReq.getPostType()), TipsConst.post_type_error);
        ValidateAssert.isTrue(StringUtils.isNoneBlank(createMoodReq.getSeqNo()), TipsConst.seq_no_deletion_error);
        ValidateAssert.isTrue(StringUtils.isNoneBlank(createMoodReq.getTitle()), TipsConst.title_deletion_error);
        ValidateAssert.isTrue(StringUtils.isNoneBlank(createMoodReq.getContent()), TipsConst.content_deletion_error);
        ValidateAssert.isTrue(PostType.getByType(createMoodReq.getPostType()) == PostType.text
                        || CollectionUtils.isNotEmpty(createMoodReq.getFileSlices())
                        && createMoodReq.getFileSlices().stream().allMatch(s -> StringUtils.isNoneBlank(s.getSeqNo())
                        && StringUtils.isNoneBlank(s.getFileName()) && StringUtils.isNoneBlank(s.getMimeType())),
                TipsConst.slice_param_deletion_error);
        log.info("createMood user {} seqNo {} postType {} title {} content {} fileSlices {} ",
                sysUserVo.getId(), createMoodReq.getSeqNo(), createMoodReq.getPostType(), createMoodReq.getTitle(),
                createMoodReq.getContent(), Jackson2Helper.toJsonString(createMoodReq.getFileSlices()));
        userMoodPort.createMood(MoodConverter.createMoodCaseVo(createMoodReq));
        return HttpResult.success();
    }

    /**
     * 包含媒体的心情的结果查询
     *
     * @param seqNo 创建帖子的流水号
     * @return
     */
    @GetMapping("/create/result-inquiry")
    public HttpResult resultInquiry(String seqNo) {
        ResultVo resultVo = userMoodPort.querySliceMergeResult(seqNo);
        if (Objects.isNull(resultVo)) {
            // 一般不会出现这种情况，所认为就是系统错误
            return new HttpResult(BusinessState.SYS_ERROR);
        } else if (Objects.isNull(resultVo.getCode())) {
            // 处理中
            return new HttpResult(BusinessState.PROCESSING);
        } else {
            // 处理成功
            return new HttpResult(resultVo.getCode(), resultVo.getMessage());
        }
    }

    @RequestMapping("/create/test")
    public HttpResult moodCreate(MultipartFile[] files, Integer postType, Integer mediaType, String title,
                                 String content) throws IOException {
        log.info("moodCreate postType {} mediaType {} title {} content {}", postType, mediaType, title, content);
        ValidateAssert.notNull(files, "无文件!");
        ValidateAssert.isTrue(MediaType.exists(mediaType), TipsConst.media_type_error);
        for(MultipartFile file : files){
            //获取图片后缀
            String imgFormat = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
            String casePath = "D:\\imgs";
            File f = new File(casePath);
            try {
                if (!f.exists()) {
                    f.mkdirs();
                }
            } catch (Exception e) {
                return new HttpResult(BusinessState.SYS_ERROR.code, TipsConst.system_error);
            }
            String name = UUID.randomUUID() + imgFormat;
            file.transferTo(new File(casePath + "\\" + name));
        }
        /// 返回生成的mood Id
        return HttpResult.success(123L);
    }

}
